package com.wtwd.device.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wtwd.device.model.vo.PageDataVO;
import com.wtwd.device.constant.RespCode;
import com.wtwd.device.model.entity.Medal;
import com.wtwd.device.model.vo.RespEntity;
import com.wtwd.device.service.WebMedalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mjy
 * @since 2022-04-11
 */
@Api(tags = "勋章模块")
@RestController
@RequestMapping("/medal")
public class WebMedalController {

    private final static Logger logger = LoggerFactory.getLogger(WebMedalController.class);

    @Autowired
    private WebMedalService medalService;

    @Value("${file.http}")
    private String fileHttp;
    @Value("${file.path}")
    private String filePath;
    @Value("${medal.image}")
    private String medalImage;

    /**
     * 分页获取勋章
     *
     * @param currentPage
     * @param pageSize
     * @param id
     * @param name
     * @return
     */
    @ApiOperation(value = "分页获取勋章", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataTypeClass = Integer.class)
            , @ApiImplicitParam(name = "pageSize", value = "页面大小", required = true, dataTypeClass = Integer.class)
            , @ApiImplicitParam(name = "id", value = "勋章类型主键id", required = true, dataTypeClass = Integer.class)
            , @ApiImplicitParam(name = "name", value = "勋章名称", required = false, dataTypeClass = Integer.class)

    })
    @GetMapping("/queryMedalPageByCondition")
    public RespEntity<PageDataVO<Medal>> queryMedalPageByCondition(@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage
            , @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
            , @RequestParam(value = "id") Long id, @RequestParam(value = "name", required = false) String name) {
        logger.info("queryMedalPageByCondition currentPage={}.pageSize={},id={},name={}", currentPage, pageSize, id, name);
        Page<Medal> page = new Page<>(currentPage, pageSize);
        Medal searchMap = new Medal();
        searchMap.setName(name);
        searchMap.setMedalTypeId(id);
        IPage<Medal> medalPage = medalService.queryMedalPageByCondition(searchMap, page);
        List<Medal> records = medalPage.getRecords();
        records.stream().forEach(e->{
            e.setMedalImg(fileHttp.concat(medalImage.concat(e.getMedalImg())));
            e.setMedalNotLight(fileHttp.concat(medalImage.concat(e.getMedalNotLight())));
        });
        PageDataVO pageDataVO = new PageDataVO();
        pageDataVO.setRows(medalPage.getRecords());
        pageDataVO.setTotal(medalPage.getTotal());
        return new RespEntity(RespCode.SUCCESS,pageDataVO);
    }


    /**
     * 添加勋章
     *
     * @param medal
     * @return
     */
    @ApiOperation(value = "添加勋章", notes = "")
    @PostMapping("/addMedal")
    public RespEntity addMedal(@RequestBody Medal medal) {
        logger.info("addMedal param ={} ", medal);
        medal.setUploadTime(new Date().getTime());
        Integer rest = medalService.addMedal(medal);
        return new RespEntity(rest>0? RespCode.SUCCESS: RespCode.FAILED);
    }

    /**
     * 删除徽章
     *
     * @param medalId
     * @return
     */
    @ApiOperation(value = "删除徽章", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "medalId", value = "徽章类类型id", required = true, dataTypeClass = Long.class)
    })
    @DeleteMapping("/deleteMedalById/{medalId}")
    public RespEntity deleteMedalById(@PathVariable("medalId") Long medalId) {
        logger.info("deleteMedalById param={}", medalId);
        Integer rest = medalService.deleteMedalById(medalId);
        return new RespEntity(rest>0? RespCode.SUCCESS: RespCode.FAILED);
    }

    @ApiOperation(value = "上传勋章图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "上传勋章图片", required = true, dataTypeClass = MultipartFile.class)
    })
    @PostMapping("/uploadMedalImage")
    public RespEntity uploadMedalImage(@RequestBody MultipartFile file, HttpServletRequest request) {
        logger.info("/uploadMedalImage");
        String path = filePath.concat(medalImage);
        File saveFile = new File(path);
        if (!saveFile.exists()) {
            //文件不存在则创建一个
            saveFile.mkdir();
        }
        String originalFilename = file.getOriginalFilename();
        String fileName = UUID.randomUUID().toString().concat(originalFilename);
        File dest = new File(path.concat(fileName));
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
            return new RespEntity(RespCode.FAILED);
        }
        return new RespEntity(RespCode.SUCCESS, fileName);
    }


}

